{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word Search\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/eRjlQE6lLaDaJ1NRR_lP0A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given a 2D array of characters, and a target string. Return whether or not the word target word exists in the matrix. Unlike a standard word search, the word must be either going left-to-right, or top-to-bottom in the matrix."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```\n",
    "[\n",
    "    ['F', 'A', 'C', 'I'],\n",
    "    ['O', 'B', 'Q', 'P'],\n",
    "    ['A', 'N', 'O', 'B'],\n",
    "    ['M', 'A', 'S', 'S']\n",
    "]\n",
    "```\n",
    "\n",
    "Given this matrix, and the target word `FOAM`, you should return `true`, as it can be found going up-to-down in the first column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "def word_search(matrix, word) -> bool:\n",
    "    words = list()\n",
    "    \n",
    "    # Find all words going left-to-right.\n",
    "    for k in matrix:\n",
    "        words.append(''.join(k))\n",
    "        pass\n",
    "    \n",
    "    # Find all words going top-to-bottom.\n",
    "    for i in range(len(matrix[0])):\n",
    "        temp = list()\n",
    "        \n",
    "        # Find all characters in the same column.\n",
    "        for k in matrix:\n",
    "            temp.append(k[i])\n",
    "            pass\n",
    "        \n",
    "        words.append(''.join(temp))\n",
    "        pass\n",
    "    \n",
    "    if word in words:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    pass\n",
    "    \n",
    "    \n",
    "matrix = [\n",
    "    ['F', 'A', 'C', 'I'],\n",
    "    ['O', 'B', 'Q', 'P'],\n",
    "    ['A', 'N', 'O', 'B'],\n",
    "    ['M', 'A', 'S', 'S']\n",
    "]\n",
    "print(word_search(matrix, 'FOAM'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
